import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'
const INITDATA = {
  tabList: [{
    name: '沙石分销',
    value: '企业名称企业名称'
  },{
    name: '水上超市',
    value: '所属行业所属行业'
  },{
    name: '工业用品',
    value: '联系人'
  },{
    name: '签到',
    value: '联系人联系人联系人'
  },{
    name: '船奇代购',
    value: '产地产地'
  },{
    name: '我要装货',
    value: '产地产地'
  },{
    name: '大船互动',
    value: '产地产地'
  },{
    name: '优惠券',
    value: '产地产地'
  }],
  dataList: [{
    name: '产品展示',
    urlData: 'product',
    data: 'productData',
    url: 'loadProductData',
  },{
    name: '设备交易',
    urlData: 'device',
    data: 'deviceData',
    url: 'loadDeviceData',
  },{
    name: '订单外协',
    urlData: 'order',
    data: 'orderData',
    url: 'loadOrderData',
  }],
  productData: [], // 产品展示
  deviceData: [], // 设备交易
  orderData: [], // 订单外协
}
export default {
  namespace: 'businessHome',
  state: {...INITDATA},
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        ...INITDATA
      }
    }
  },
  effects: {
    // 获取列表数据
    * loadProductData({payload = {}}, {select, put, call}) {
      const param = {
        limit: 6,
        offset: 0,
      }
      const {data} = yield call(request, {
        url: SERVERURL + '/service/product/getPage',
        data: param,
        method: 'POST',
      })

      if (data && data.rows instanceof Array) {
        const d = data.rows || {};
        yield put({
          type: 'updateState',
          payload: {
            productData: d,
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    // 获取列表数据
    * loadDeviceData({payload = {}}, {select, put, call}) {
      const param = {
        limit: 6,
        offset: 0,
      }
      const {data} = yield call(request, {
        url: SERVERURL + '/service/device/getPage',
        data: param,
        method: 'POST',
      })
      if (data && data.rows instanceof Array) {
        const d = data.rows || {};
        yield put({
          type: 'updateState',
          payload: {
            deviceData: d,
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    // 获取列表数据
    * loadOrderData({payload = {}}, {select, put, call}) {
      const param = {
        limit: 6,
        offset: 0,
      }
      const {data} = yield call(request, {
        url: SERVERURL + '/service/order/getPage',
        data: param,
        method: 'POST',
      })
      if (data && data.rows instanceof Array) {
        const d = data.rows || {};
        yield put({
          type: 'updateState',
          payload: {
            orderData: d,
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
  }
};
